package com.kiragame.xchat.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;

import com.kiragame.xchat.R;
import com.kiragame.xchat.utils.DataManager;

public class RateAppDialogFragment extends DialogFragment {
	private Button btRate;
	private Button btLater;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_rate_app, container);
		findViewIds(view);
		init();
		setEvent();
		return view;
	}

	private void init() {
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	private void setEvent() {
		btRate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				rateApp();
				DataManager.getInstance(getActivity()).setRatedApp(true);
				dismiss();
			}
		});

		btLater.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
	}

	private void findViewIds(View view) {
		btRate = (Button) view.findViewById(R.id.bt_rate);
		btLater = (Button) view.findViewById(R.id.bt_later);
	}

	@Override
	public void onDismiss(DialogInterface dialog) {
		super.onDismiss(dialog);
	}

	@Override
	public void onCancel(DialogInterface dialog) {
		super.onCancel(dialog);
	}

	public void rateApp() {
		final String packageName = getActivity().getApplicationContext()
				.getPackageName();
		if (packageName == null)
			return;
		Intent t = new Intent(Intent.ACTION_VIEW);
		t.setData(Uri.parse("market://details?id=" + packageName));
		getActivity().startActivity(t);
	}
}
